<?php
if (!defined('SMF'))
	die('Hacking attempt...');

global $smcFunc, $txt, $scripturl, $user_info;
global $dbtables, $universe_size;

$module = (isset($_POST['menu'])) ? $_POST['menu'] : '';

if (empty($module))
{
	echo '
					select a function from the list below:<br>
					<form action="', $scripturl, '?action=admin;area=blacknovasettings;sa=gameadmin" method="post">
						<select name="menu">
							<option value="univedit">Universe editor</option>
							<option value="sectedit">Sector editor</option>
							<option value="planedit">Planet editor</option>
							<option value="linkedit">Link editor</option>
							<option value="zoneedit">Zone editor</option>
							<option value="logview">Log Viewer</option>
							<option value="logview">Game Reset</option>
						</select>
						<input type="submit" value="submit">
					</form>';
}
else
{
	$button_main = true;

    if ($module == "univedit")
	{
		echo '
					<b>Universe editor</b>
					<br>Expand or Contract the Universe <br>';
		
		$action = (isset($_POST['universe_action'])) ? $_POST['universe_action'] : '';
		
		if (empty($action))
		{
			echo '
					<form action="', $scripturl, '?action=admin;area=blacknovasettings;sa=gameadmin" method=post>
						Universe Size: <input type="text" name="radius" value="', $universe_size, '">
						<input type="hidden" name="menu" value="univedit">
						<input type="hidden" name="universe_action" value="doexpand"> 
						<input type="submit" value="Change Size">
					</form>';
		}
		elseif ($action == "doexpand")
		{
			echo '
					<br><p><b>Be sure to update your config.php file with the new universe_size value</b></p><br>';
			srand((double)microtime()*1000000);
			
			$radius = (isset($_POST['radius'])) ? $_POST['radius'] : die();
			
			$result = $smcFunc['db_query']('', '
				SELECT sector_id
				FROM {raw:db_universe}
				ORDER BY sector_id ASC',
				array(
					'db_universe' => $dbtables['universe']
				)
			);
			
			while ($row = $smcFunc['db_fetch_assoc']($result))
			{
				$distance = rand(1,$radius);
				$update = $smcFunc['db_query']('', '
					UPDATE {raw:db_universe}
					SET distance = {int:distance}
					WHERE sector_id = {int:sector_id}',
					array(
						'db_universe' => $dbtables['universe'],
						'distance' => $distance,
						'sector_id' => $row['sector_id']
					)
				);
				
				echo '
					Updated sector ', $row['sector_id'], ' set to ', $distance, '<br>';
			}
			
			$smcFunc['db_free_result']($result);
		}
	}
    elseif ($module == "sectedit")
    {
		echo "<H2>Sector editor</H2>";
		echo "<form action=admin.php method=POST>";
		if(empty($sector))
		{
			echo "<H5>Note: Cannot Edit Sector 0</H5>";
			echo "<select SIZE=20 name=sector>";
			$res = $db->Execute("select sector_id FROM $dbtables[universe] ORDER BY sector_id");
			while(!$res->EOF)
			{
				$row=$res->fields;
				echo "<option value=$row[sector_id]> $row[sector_id] </option>";
				$res->MoveNext();
			}
			echo "</select>";
			echo "&nbsp;<input type=submit value=Edit>";
		}
		else
		{
			if(empty($operation))
			{
				$res = $db->Execute("select * FROM $dbtables[universe] WHERE sector_id=$sector");
				$row = $res->fields;

				echo "<TABLE BORDER=0 CELLSPACING=2 CELLPADDING=2>";
				echo "<TR><TD><tt>          Sector ID  </tt></TD><TD><FONT COLOR=#66FF00>$sector</FONT></TD>";
				echo "<TD ALIGN=Right><tt>  Sector name</tt></TD><TD><input type=TEXT SIZE=15 name=sector_name value=\"$row[sector_name]\"></TD>";
				echo "<TD ALIGN=Right><tt>  Zone ID    </tt></TD><TD>";
				echo "<select SIZE=1 name=zone_id>";
				$ressubb = $db->Execute("select zone_id,zone_name FROM $dbtables[zones] ORDER BY zone_name");
				while(!$ressubb->EOF)
				{
				$rowsubb=$ressubb->fields;
				if ($rowsubb[zone_id] == $row[zone_id])
				{
				echo "<option selected=$rowsubb[zone_id] value=$rowsubb[zone_id]>$rowsubb[zone_name]</option>";
				} else {
				echo "<option value=$rowsubb[zone_id]>$rowsubb[zone_name]</option>";
				}
				$ressubb->MoveNext();
				}
				echo "</select></TD></TR>";
				echo "<TR><TD><tt>          Beacon     </tt></TD><TD COLSPAN=5><input type=TEXT SIZE=70 name=beacon value=\"$row[beacon]\"></TD></TR>";
				echo "<TR><TD><tt>          Distance   </tt></TD><TD><input type=TEXT SIZE=9 name=distance value=\"$row[distance]\"></TD>";
				echo "<TD ALIGN=Right><tt>  Angle1     </tt></TD><TD><input type=TEXT SIZE=9 name=angle1 value=\"$row[angle1]\"></TD>";
				echo "<TD ALIGN=Right><tt>  Angle2     </tt></TD><TD><input type=TEXT SIZE=9 name=angle2 value=\"$row[angle2]\"></TD></TR>";
				echo "<TR><TD COLSPAN=6>    <HR>       </TD></TR>";
				echo "</TABLE>";

				echo "<TABLE BORDER=0 CELLSPACING=2 CELLPADDING=2>";
				echo "<TR><TD><tt>          Port type  </tt></TD><TD>";
				echo "<select SIZE=1 name=port_type>";
				$oportnon = $oportorg = $oportore = $oportgoo = $oportene = "value";
				if ($row[port_type] == "none") $oportnon = "selected=none value";
				if ($row[port_type] == "organics") $oportorg = "selected=organics value";
				if ($row[port_type] == "ore") $oportore = "selected=ore value";
				if ($row[port_type] == "goods") $oportgoo = "selected=goods value";
				if ($row[port_type] == "energy") $oportene = "selected=energy value";
				echo "<option $oportnon=none>none</option>";
				echo "<option $oportorg=organics>organics</option>";
				echo "<option $oportore=ore>ore</option>";
				echo "<option $oportgoo=goods>goods</option>";
				echo "<option $oportene=energy>energy</option>";
				echo "</select></TD>";
				echo "<TD ALIGN=Right><tt>  Organics   </tt></TD><TD><input type=TEXT SIZE=9 name=port_organics value=\"$row[port_organics]\"></TD>";
				echo "<TD ALIGN=Right><tt>  Ore        </tt></TD><TD><input type=TEXT SIZE=9 name=port_ore value=\"$row[port_ore]\"></TD>";
				echo "<TD ALIGN=Right><tt>  Goods      </tt></TD><TD><input type=TEXT SIZE=9 name=port_goods value=\"$row[port_goods]\"></TD>";
				echo "<TD ALIGN=Right><tt>  Energy     </tt></TD><TD><input type=TEXT SIZE=9 name=port_energy value=\"$row[port_energy]\"></TD></TR>";
				echo "<TR><TD COLSPAN=10>   <HR>       </TD></TR>";
				echo "</TABLE>";

				echo "<br>";
				echo "<input type=hidden name=sector value=$sector>";
				echo "<input type=hidden name=operation value=save>";
				echo "<input type=submit SIZE=1 value=Save>";
			}
			elseif($operation == "save")
			{
				// update database
				$secupdate = $db->Execute("UPDATE $dbtables[universe] SET sector_name='$sector_name',zone_id='$zone_id',beacon='$beacon',port_type='$port_type',port_organics='$port_organics',port_ore='$port_ore',port_goods='$port_goods',port_energy='$port_energy',distance='$distance',angle1='$angle1',angle2='$angle2' WHERE sector_id=$sector");
				if(!$secupdate)
				{
					echo "Changes to Sector record have FAILED Due to the following Error:<br><br>";
					echo $db->ErrorMsg() . "<br>";
				}
				else
					echo "Changes to Sector record have been saved.<br><br>";
				
				echo "<input type=submit value=\"Return to Sector editor\">";
				
				$button_main = false;
			}
			else
				echo "Invalid operation";
		}
		echo "<input type=hidden name=menu value=sectedit>";
		echo "<input type=hidden name=swordfish value=$swordfish>";
		echo "</form>";
    }
    elseif ($module == "planedit")
	{
		echo "<H2>Planet editor</H2>";
		echo "<form action=admin.php method=POST>";
		if(empty($planet))
		{
		echo "<select SIZE=15 name=planet>";
		$res = $db->Execute("select planet_id, name, sector_id FROM $dbtables[planets] ORDER BY sector_id");
		while(!$res->EOF)
		{
		$row=$res->fields;
		if($row[name] == "")

		$row[name] = "Unnamed";

		echo "<option value=$row[planet_id]> $row[name] in sector $row[sector_id] </option>";
		$res->MoveNext();
		}
		echo "</select>";
		echo "&nbsp;<input type=submit value=Edit>";
		}
		else
		{
		if(empty($operation))
		{
		$res = $db->Execute("select * FROM $dbtables[planets] WHERE planet_id=$planet");
		$row = $res->fields;

		echo "<TABLE BORDER=0 CELLSPACING=2 CELLPADDING=2>";
		echo "<TR><TD><tt>          Planet ID  </tt></TD><TD><FONT COLOR=#66FF00>$planet</FONT></TD>";
		echo "<TD ALIGN=Right><tt>  Sector ID  </tt><input type=TEXT SIZE=5 name=sector_id value=\"$row[sector_id]\"></TD>";
		echo "<TD ALIGN=Right><tt>  Defeated   </tt><input type=CHECKBOX name=defeated value=ON " . CHECKED($row[defeated]) . "></TD></TR>";
		echo "<TR><TD><tt>          Planet name</tt></TD><TD><input type=TEXT SIZE=15 name=name value=\"$row[name]\"></TD>";
		echo "<TD ALIGN=Right><tt>  Base       </tt><input type=CHECKBOX name=base value=ON " . CHECKED($row[base]) . "></TD>";
		echo "<TD ALIGN=Right><tt>  Sells      </tt><input type=CHECKBOX name=sells value=ON " . CHECKED($row[sells]) . "></TD></TR>";
		echo "<TR><TD COLSPAN=4>    <HR>       </TD></TR>";
		echo "</TABLE>";

		echo "<TABLE BORDER=0 CELLSPACING=2 CELLPADDING=2>";
		echo "<TR><TD><tt>          Planet Owner</tt></TD><TD>";
							  echo "<select SIZE=1 name=owner>";
							  $ressuba = $db->Execute("select ship_id,character_name FROM $dbtables[ships] ORDER BY character_name");
							  echo "<option value=0>No One</option>";
							  while(!$ressuba->EOF)
							  {
							  $rowsuba=$ressuba->fields;
							  if ($rowsuba[ship_id] == $row[owner])
								{
								echo "<option selected=$rowsuba[ship_id] value=$rowsuba[ship_id]>$rowsuba[character_name]</option>";
								} else {
								echo "<option value=$rowsuba[ship_id]>$rowsuba[character_name]</option>";
								}
								$ressuba->MoveNext();
							  }
							  echo "</select></TD>";
		echo "<TD ALIGN=Right><tt>  Organics   </tt></TD><TD><input type=TEXT SIZE=9 name=organics value=\"$row[organics]\"></TD>";
		echo "<TD ALIGN=Right><tt>  Ore        </tt></TD><TD><input type=TEXT SIZE=9 name=ore value=\"$row[ore]\"></TD>";
		echo "<TD ALIGN=Right><tt>  Goods      </tt></TD><TD><input type=TEXT SIZE=9 name=goods value=\"$row[goods]\"></TD>";
		echo "<TD ALIGN=Right><tt>  Energy     </tt></TD><TD><input type=TEXT SIZE=9 name=energy value=\"$row[energy]\"></TD></TR>";
		echo "<TR><TD><tt>          Planet Corp</tt></TD><TD><input type=TEXT SIZE=5 name=corp value=\"$row[corp]\"></TD>";
		echo "<TD ALIGN=Right><tt>  Colonists  </tt></TD><TD><input type=TEXT SIZE=9 name=colonists value=\"$row[colonists]\"></TD>";
		echo "<TD ALIGN=Right><tt>  Credits    </tt></TD><TD><input type=TEXT SIZE=9 name=credits value=\"$row[credits]\"></TD>";
		echo "<TD ALIGN=Right><tt>  Fighters   </tt></TD><TD><input type=TEXT SIZE=9 name=fighters value=\"$row[fighters]\"></TD>";
		echo "<TD ALIGN=Right><tt>  Torpedoes  </tt></TD><TD><input type=TEXT SIZE=9 name=torps value=\"$row[torps]\"></TD></TR>";
		echo "<TR><TD COLSPAN=2><tt>Planet Production</tt></TD>";
		echo "<TD ALIGN=Right><tt>  Organics   </tt></TD><TD><input type=TEXT SIZE=9 name=prod_organics value=\"$row[prod_organics]\"></TD>";
		echo "<TD ALIGN=Right><tt>  Ore        </tt></TD><TD><input type=TEXT SIZE=9 name=prod_ore value=\"$row[prod_ore]\"></TD>";
		echo "<TD ALIGN=Right><tt>  Goods      </tt></TD><TD><input type=TEXT SIZE=9 name=prod_goods value=\"$row[prod_goods]\"></TD>";
		echo "<TD ALIGN=Right><tt>  Energy     </tt></TD><TD><input type=TEXT SIZE=9 name=prod_energy value=\"$row[prod_energy]\"></TD></TR>";
		echo "<TR><TD COLSPAN=6><tt>Planet Production</tt></TD>";
		echo "<TD ALIGN=Right><tt>  Fighters   </tt></TD><TD><input type=TEXT SIZE=9 name=prod_fighters value=\"$row[prod_fighters]\"></TD>";
		echo "<TD ALIGN=Right><tt>  Torpedoes  </tt></TD><TD><input type=TEXT SIZE=9 name=prod_torp value=\"$row[prod_torp]\"></TD></TR>";
		echo "<TR><TD COLSPAN=10>   <HR>       </TD></TR>";
		echo "</TABLE>";

		echo "<br>";
		echo "<input type=hidden name=planet value=$planet>";
		echo "<input type=hidden name=operation value=save>";
		echo "<input type=submit SIZE=1 value=Save>";
		}
		elseif($operation == "save")
		{
		// update database
		$_defeated = empty($defeated) ? "N" : "Y";
		$_base = empty($base) ? "N" : "Y";
		$sells = empty($sells) ? "N" : "Y";
		$planupdate = $db->Execute("UPDATE $dbtables[planets] SET sector_id='$sector_id',defeated='$_defeated',name='$name',base='$_base',sells='$_sells',owner='$owner',organics='$organics',ore='$ore',goods='$goods',energy='$energy',corp='$corp',colonists='$colonists',credits='$credits',fighters='$fighters',torps='$torps',prod_organics='$prod_organics',prod_ore='$prod_ore',prod_goods='$prod_goods',prod_energy='$prod_energy',prod_fighters='$prod_fighters',prod_torp='$prod_torp' WHERE planet_id=$planet");
		if(!$planupdate) {
		echo "Changes to Planet record have FAILED Due to the following Error:<br><br>";
		echo $db->ErrorMsg() . "<br>";
		} else {
		echo "Changes to Planet record have been saved.<br><br>";
		}
		echo "<input type=submit value=\"Return to Planet editor\">";
		$button_main = false;
		}
		else
		{
		echo "Invalid operation";
		}
		}
		echo "<input type=hidden name=menu value=planedit>";
		echo "<input type=hidden name=swordfish value=$swordfish>";
		echo "</form>";
    }
    elseif ($module == "linkedit")
      echo "<b>Link editor</b>";
    elseif ($module == "zoneedit")
	{
		echo "<b>Zone editor</b>";
		echo "<br>";
		echo "<form action=admin.php method=POST>";
		if(empty($zone))
		{
		echo "<select SIZE=20 name=zone>";
		$res = $db->Execute("select zone_id,zone_name FROM $dbtables[zones] ORDER BY zone_name");
		while(!$res->EOF)
		{
		$row=$res->fields;
		echo "<option value=$row[zone_id]>$row[zone_name]</option>";
		$res->MoveNext();
		}
		echo "</select>";
		echo "<input type=hidden name=operation value=editzone>";
		echo "&nbsp;<input type=submit value=Edit>";

		}
		else
		{
		if($operation == "editzone")
		{
		$res = $db->Execute("select * FROM $dbtables[zones] WHERE zone_id=$zone");
		$row = $res->fields;
		echo "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=5>";
		echo "<TR><TD>Zone ID</TD><TD>$row[zone_id]</TD></TR>";
		echo "<TR><TD>Zone name</TD><TD><input type=TEXT name=zone_name value=\"$row[zone_name]\"></TD></TR>";
		echo "<TR><TD>Allow Beacon</TD><TD><input type=CHECKBOX name=zone_beacon value=ON " . CHECKED($row[allow_beacon]) . "></TD>";
		echo "<TR><TD>Allow Attack</TD><TD><input type=CHECKBOX name=zone_attack value=ON " . CHECKED($row[allow_attack]) . "></TD>";
		echo "<TR><TD>Allow WarpEdit</TD><TD><input type=CHECKBOX name=zone_warpedit value=ON " . CHECKED($row[allow_warpedit]) . "></TD>";
		echo "<TR><TD>Allow Planet</TD><TD><input type=CHECKBOX name=zone_planet value=ON " . CHECKED($row[allow_planet]) . "></TD>";
		echo "</TABLE>";
		echo "<TR><TD>Max Hull</TD><TD><input type=TEXT name=zone_hull value=\"$row[max_hull]\"></TD></TR>";
		echo "<br>";
		echo "<input type=hidden name=zone value=$zone>";
		echo "<input type=hidden name=operation value=savezone>";
		echo "<input type=submit value=Save>";
		}
		elseif($operation == "savezone")
		{
		// update database
		$_zone_beacon = empty($zone_beacon) ? "N" : "Y";
		$_zone_attack = empty($zone_attack) ? "N" : "Y";
		$_zone_warpedit = empty($zone_warpedit) ? "N" : "Y";
		$_zone_planet = empty($zone_planet) ? "N" : "Y";
		$db->Execute("UPDATE $dbtables[zones] SET zone_name='$zone_name',allow_beacon='$_zone_beacon' ,allow_attack='$_zone_attack' ,allow_warpedit='$_zone_warpedit' ,allow_planet='$_zone_planet', max_hull='$zone_hull' WHERE zone_id=$zone");
		echo "Changes saved<br><br>";
		echo "<input type=submit value=\"Return to Zone Editor \">";
		$button_main = false;
		}
		else
		{
		echo "Invalid operation";
		}
		}
		echo "<input type=hidden name=menu value=zoneedit>";
		echo "<input type=hidden name=swordfish value=$swordfish>";
		echo "</form>";
    }
    elseif ($module == "logview")
    {
		echo '
						<form action="', $scripturl, '?action=admin;area=blacknovasettings;sa=adminlog" method="post">
							<input type="hidden" name="player" value="0">
							<input type="submit" value="View Admin Log">
						</form>
						<br><br>
						<form action="', $scripturl, '?action=admin;area=blacknovasettings;sa=adminlog" method="post">
							<select name="player">';
		
		$result = $smcFunc['db_query']('', '
			SELECT ship_id, character_name
			FROM {raw:db_ships}
			ORDER BY character_name ASC',
			array(
				'db_ships' => $dbtables['ships']
			)
		);
		
		while ($row = $smcFunc['db_fetch_assoc']($result))
		{
			echo '
								<option value="', $row['ship_id'], '">', $row['character_name'], '</option>';
		}

		echo '
							</select>
								<input type="submit" value="View Player Log">
							</form>';
    }
    else
      echo "Unknown action!";

    if ($button_main)
    {
		echo '
					<p>
					<form action="', $scripturl, '?action=admin;area=blacknovasettings;sa=gameadmin" method="post">
						<input type="submit" value="Return to main menu">
					</form>';
    }
}

function CHECKED($yesno)
{
	return(($yesno == "Y") ? "CHECKED" : "");
}

function YESNO($onoff)
{
	return(($onoff == "ON") ? "Y" : "N");
}
?>
